<%@ page language="java" contentType="text/html; charset=US-ASCII"
	pageEncoding="US-ASCII" session="true"%>
<%@ page import="edu.unsw.comp9321.common.CarBean"%>
<%@ page import="edu.unsw.comp9321.jdbc.CarTypeDTO"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.ArrayList"%>
<jsp:useBean id="searchResult"
	class="edu.unsw.comp9321.common.SearchBean" scope="session" />
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Welcome to CarRental</title>
<link href="CSS.css" rel="stylesheet" type="text/css">
</head>
<body>

	<div id="pagehdr">
		<h1>Car Rental reservation system</h1>
	</div>
	<%
	String errorMsg = (String) request.getAttribute("violation");
	if (errorMsg != null) {
		if (!errorMsg.equals("noViolation")) {
			out.write("<br/><span style=\"color:red\"><strong>"	+ errorMsg +"</strong></span><br/>");
		}
	%>
		<br/>
		<form name="searchResult" action="controller" method="POST">
			<table border="1" style="text-align:center">
			<tr>
				<th>Class</th>
				<th>Make and Model</th>
				<th>Body Type</th>
				<th>Transmission Type</th>
				<th>Daily Rate</th>
				<th>Image</th>
				<th>Choose car</th>
			</tr>
	<%
			List<CarTypeDTO> carsShown = new ArrayList<CarTypeDTO>();
				for(CarBean car: searchResult.getCars()){
					if(!carsShown.contains(car.getCarType())){
						carsShown.add(car.getCarType());
						out.write("<tr>");
						out.write("<td>"+car.getCarType().getTypeClass()+"</td>");
						out.write("<td>"+car.getCarType().getMakeModel()+"</td>");
						out.write("<td>"+car.getCarType().getBodyType()+"</td>");
						if (car.getCarType().isAutomatic()) {
							out.write("<td>Automatic</td>");
						} else if (!car.getCarType().isAutomatic()) {
							out.write("<td>Manual</td>");
						}
						out.write("<td>$ "+car.getCarType().getDailyRate()+"</td>");
						out.write("<td><img src=\""+request.getContextPath()+"/cars_pics/carType_"+car.getCarType().getId()+"_picture.jpg\" height=\"100\"/></td>");
						out.write("<td><input type=\"radio\" name=\"chosenCar\" value=\""+car.getCar().getCarId()+"\"><br/></td>");
						out.write("</tr>");
					}
				}
	%>
			</table>
			<br/>
			<input type="hidden" name="op" value="chooseCar"/> 
			<input type="submit" name="chooseSelectedCar" title="Choose selected car" value="Choose selected car"/>
		</form>
	<%
	} else {
	%>
			<span style="color:red">
				<strong>Sorry, an error occurred. Please search again.</strong>
			</span>
			<form action="controller" method="POST">
				<input type="hidden" name="op" value="searchAgain" />
				<input type="submit" value="Search again"/>
			</form>
	<%
		}
	%>
	</body>
</html>